from concurrence import unittest
from concurrence.io import IOStream
from concurrence.io.buffered import Buffer, BufferedReader

class TestStream(IOStream):
    def __init__(self, test_reads):
        self._test_reads = test_reads
        
    def read(self, buffer, timeout = -1.0):
        if self._test_reads:
            test_bytes = self._test_reads[0]
            buffer.write_bytes(test_bytes)
            self._test_reads = self._test_reads[1:]
            return len(test_bytes)
        else:
            raise EOFError()


class TestBuffered(unittest.TestCase):
    def testReadLines(self):
        
        b = Buffer(1024)

        test_stream = TestStream(['hello world!\nTest\n\nPiet\nKlaas', '\nPietBlaat\n'])
        
        f = BufferedReader(test_stream, b).file()
        
        lines = f.readlines()

        self.assertEquals('hello world!\n', lines.next())
        self.assertEquals('Test\n', lines.next())
        self.assertEquals('\n', lines.next())
        self.assertEquals('Piet\n', lines.next())
        self.assertEquals('Klaas\n', lines.next())
        self.assertEquals('PietBlaat\n', lines.next())
        
if __name__ == '__main__':
    unittest.main(timeout = 10)
